// pages/mine/mine.js

import UserTool from "../../utils/UserTool.js"

Page({

    /**
     * 页面的初始数据
     */
    data: {
        isLogin: false,
        token: "",
        userInfo: {
            avatar: "../../images/tabbar/tab_mine.png",
            username: "您还未登录",
            realName: "",
            id: "",
        },
        defaultUserInfo: {
            avatar: "../../images/tabbar/tab_mine.png",
            username: "您还未登录",
            realName: "",
            id: "",
        },
        isShowInput: false,
        number: ""
    },
    setValue(e) {
        this.setData({
            number: e.detail.value
        })
    },
    cancel() {
        this.setData({
            isShowInput: false,
        })
    },
    confirmAcceptance() {
        this.setData({
            isShowInput: false,
        })
        wx.request({
            url: getApp().globalData.baseUrl + "user/info",
            method: "PUT",
            header: {
                token: getApp().globalData.token
            },
            data: {
                realName: this.data.number
            },
            success: (res) => {
                console.log(res);
                this.onShow();
            },
            fail: (err) => {
                console.log(err);
            }
        })
    },
    realname() {
        this.setData({
            isShowInput: true
        })
    },

    chooseWay(e) {
        if (getApp().globalData.isLogin == false) {
            wx.showModal({
                title: '提示',
                content: '使用微信号一键登录',
                success: (res) => {
                    if (res.confirm) {
                        this.loginByWX();
                    } else {
                        this.goToLogin();
                    }
                }
            })
        } else {
            wx.showActionSheet({
                itemList: ['相册'],
                itemColor: '#007aff',
                success: (res) => {
                    console.log(res.tapIndex);
                    if (res.tapIndex === 0) {
                        wx.chooseMedia({
                            count: 1,
                            mediaType: ['image'],
                            sourceType: ['album'],
                            success: (res) => {
                                console.log(res.tempFiles[0].tempFilePath);
                                wx.uploadFile({
                                    url: getApp().globalData.baseUrl + "upload/image",
                                    filePath: res.tempFiles[0].tempFilePath,
                                    name: 'image',
                                    header: {
                                        "Content-Type": "multipart/form-data",
                                        "token": getApp().globalData.token
                                    },
                                    success: (res) => {
                                        let data = JSON.parse(res.data);
                                        wx.request({
                                            url: getApp().globalData.baseUrl + "user/info",
                                            method: "PUT",
                                            header: {
                                                token: getApp().globalData.token
                                            },
                                            data: {
                                                avatar: data.data.path
                                            },
                                            success: (res) => {
                                                console.log(res);
                                                this.onShow();
                                                // console.log(this);
                                            },
                                            fail: (err) => {
                                                console.log(err);
                                            }
                                        })
                                    },
                                    fail: (err) => {
                                        console.log(err);
                                    }
                                })
                            }
                        })
                    }
                }
            })
        }
    },

    exitLogin() {
        if (this.data.isLogin == true) {
            this.setData({
                isLogin: false,
            })
            getApp().globalData.isLogin = false;
            this.setData({
                userInfo: this.data.defaultUserInfo
            })
            UserTool.exitLogin();
        }
    },

    goToLogin() {
        wx.navigateTo({
            url: '../independent/login/login',
        })
    },

    loginByWX() {
        wx.getUserProfile({
            desc: '用于登录注册',
            success: (res) => {
                wx.login({
                    timeout: 2000,
                    success: (res) => {
                        let test = UserTool.loginByWX(res.code);
                        test.then((res) => {
                            if (res.code == 200) {
                                getApp().globalData.token = res.data.token;
                                getApp().globalData.isLogin = true;
                                wx.setStorageSync('token', res.data.token)
                                this.onShow();
                            } else {
                                this,
                                this.goToLogin();
                                wx.showToast({
                                    title: "您还未绑定账号，请先登录或者注册一个账号",
                                    icon: 'none',
                                    duration: 3000
                                });
                            }
                        })
                    },
                    fail: (err) => {
                        console.log(err);
                    }
                })
            },
            fail: (err) => {
                console.log(err);
            }
        })
    },

    bindWX() {
        wx.login({
            timeout: 2000,
            success: (res) => {
                UserTool.bindWX(res.code, getApp().globalData.token)
            },
            fail: (err) => {
                console.log(err);
            }
        })
    },

    /**
     * 生命周期函数--监听页面显示
     */
    onShow() {
        console.log(getApp().globalData.token);
        let isLogin = getApp().globalData.isLogin;
        this.setData({
            isLogin: isLogin
        })
        if (isLogin) {

            getApp().globalData.vxios.defaults.header = {
                token: getApp().globalData.token
            }
            wx.request({
                url: getApp().globalData.baseUrl + "user/info",
                header: {
                    'context-type': 'application/json',
                    'token': getApp().globalData.token
                },
                method: "GET",
                success: (res) => {

                    if (res.data.code == 200) {
                        console.log(res.data.data);
                        if (res.data.data.avatar === "") {
                            res.data.data.avatar = getApp().globalData.iconUrlHead + "com_avatar.png";
                        }
                        getApp().globalData.userInfo = res.data.data;
                        this.setData({
                            userInfo: res.data.data
                        })
                        wx.login({
                            timeout: 2000,
                            success: (res) => {
                                let test = UserTool.loginByWX(res.code);
                                test.then((res) => {
                                    if (res.code == 500) {
                                        wx.showModal({
                                            title: '提示',
                                            content: '当前账号未绑定微信号，绑定之后可以使用微信号一键登录',
                                            success: (res) => {
                                                if (res.confirm) {
                                                    this.bindWX();
                                                }
                                            }
                                        })
                                    }
                                })
                            },
                            fail: (err) => {
                                console.log(err);
                            }
                        })
                    }
                },
                fail: (err) => {
                    console.log(err);
                }
            })
        }
    },

    /**
     * 生命周期函数--监听页面隐藏
     */
    onHide() {

    },

    /**
     * 生命周期函数--监听页面卸载
     */
    onUnload() {

    },

    /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
    onPullDownRefresh() {

    },

    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom() {

    },

    /**
     * 用户点击右上角分享
     */
    onShareAppMessage() {

    }
})